package com.mk.util;
/**
* @author 作者:秋哲    E-mail:2810201146@qq.com:
* @version 创建时间：2020年8月24日 下午3:05:23
* @description: 
*/

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeDriverService.Builder;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebSelenium {
	/**
	 * 浏览器
	 */
	public WebDriver web;
	/**
	 * js 执行器
	 */
	public JavascriptExecutor js;
	
	private WebDriverWait wait;
	
	public WebSelenium() {
		web = this.getWebChrome(false);
//		web = this.getLocaChrome();
		wait = new WebDriverWait(this.web, 40);
		js = (JavascriptExecutor) web;
	}
	
	public WebDriver getLocaChrome() {
		System.setProperty("webdriver.chrome.driver","data/chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
		return new ChromeDriver(options);
	}
	
	public WebDriver getWebChrome(boolean show) {
		try {
			System.setProperty("webdriver.chrome.driver", "data/chromedriver.exe");
			ChromeOptions options = new ChromeOptions();
			if(!show)options.addArguments("-headless");
			web = new ChromeDriver(options);
			web.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//			Dimension dimension = new Dimension(100, 300);
//			web.manage().window().setSize(dimension);
			return web;
		} catch (Exception e) {
//			new JOptionPane().showMessageDialog(null,"谷歌浏览器加载失败","错误" ,JOptionPane.ERROR_MESSAGE);
			System.out.println("谷歌浏览器加载失败");
			return null;
		}
	}
	
	public ChromeDriverService getWebChromeDriverService() {
		ChromeDriverService service = new ChromeDriverService.Builder()
				.usingDriverExecutable(new File("data/chromedriver.exe"))
				.usingAnyFreePort().build();
		try {
			service.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		ChromeOptions options = new ChromeOptions();
//		options.addArguments("-headless");
		web = new ChromeDriver(service,options);
		web.get("https://blog.csdn.net/zhuyiquan/article/details/80148767");
		web.get("https://blog.csdn.net/zhuyiquan/article/details/80148767");
		// 关闭 ChromeDriver 接口
		web.quit();
//		service.stop();
		return service;
	}
	
	public static void main(String[] args) {
//		System.setProperty("webdriver.chrome.driver", "data/chromedriver.exe");
//		ChromeOptions options = new ChromeOptions();
//		ChromeDriver chromeDriver = new ChromeDriver(options);
//		chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//		chromeDriver.get("https://blog.csdn.net/zhuyiquan/article/details/80148767");
//		chromeDriver.quit();
		System.out.println("测试获取web");
		WebSelenium webSelenium = new WebSelenium();
		webSelenium.web.get("https://blog.csdn.net/zhuyiquan/article/details/80148767");
	}
    private  WebDriver getWebEdge() {
    // 	指定MicrosoftWebDriver路径
      System.setProperty("webdriver.edge.driver", ".\\drivers\\MicrosoftWebDriver.exe");
      web = new EdgeDriver();
      return web;
  }
    
    private By getBy(String value, String type) {
    	/**
    	 *  返回指定元素
         *	id name class_name 
         *	tag_name link_text partial_link_text 
         *	xpath css_selector
    	 */
		By xpath = null;
		switch (type) {
		case "id":
			xpath = By.xpath(value);
			break;
		case "name":
			xpath = By.name(value);
			break;
		case "class_name":
			xpath = By.className(value);
			break;
		case "tag_name":
			xpath = By.tagName(value);
			break;
		case "link_text":
			xpath = By.linkText(value);
			break;
		case "partial_link_text":
			xpath = By.partialLinkText(value);
			break;
		case "xpath":
			xpath = By.xpath(value);
			break;
		case "css_selector":
			xpath = By.cssSelector(value);
			break;
		default:
			break;
		}
		return xpath;
	}
    
    private By getBy(String value) {
    	return this.getBy(value,"css_selector");
    }
    
    public WebElement getElement(String value, String type) {
    	WebElement element = null;
    	By by = this.getBy(value,type);
    	element = web.findElement(by);
    	return element;
    }
    public WebElement getElement(String value) {
    	return this.getElement(value,"css_selector");
    }
    
    
    public WebElement waitElement(String value, String type) {
    	// http://www.mamicode.com/info-detail-2353800.html
    	WebElement until = wait.until(ExpectedConditions.visibilityOfElementLocated(this.getBy(value, type)));
    	return until;
    }
    public WebElement waitElement(String value) {
    	return this.waitElement(value,"css_selector");
    }
    
    
    
    
    public void clickWait(String value) {
    	this.waitElement(value).click();
	}
	public void click(String value) {
		this.getElement(value).click();
	}
	public void click(String value,String type) {
		this.getElement(value,type).click();
	}
	
	
	
	public String getValue(String value) {
		return this.getElement(value).getAttribute("value");
	}
	public String getText(String value) {
		return this.getElement(value).getText();
	}
	public String getText(String value,String type) {
		return this.getElement(value,type).getText();
	}
	
	
	public void sendKey(String value,String str) {
		WebElement element = getElement(value);
		element.sendKeys(str);
	}
	public void sendKeyAndClear(String value,String str) {
		WebElement element = getElement(value);
		element.clear();
		element.sendKeys(str);
	}
}
